/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.drhis.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.hcht.xrlh.drhis.entity.DrHis;
import com.hcht.xrlh.drhis.service.DrHisService;

/**
 * 随机抽取历史Controller
 * @author lrz
 * @version 2016-12-26
 */
@Controller
@RequestMapping(value = "${adminPath}/drhis/drHis")
public class DrHisController extends BaseController {

	@Autowired
	private DrHisService drHisService;
	
	@ModelAttribute
	public DrHis get(@RequestParam(required=false) String id) {
		DrHis entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drHisService.get(id);
		}
		if (entity == null){
			entity = new DrHis();
		}
		return entity;
	}
	
	@RequiresPermissions("drhis:drHis:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrHis drHis, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrHis> page = drHisService.findPage(new Page<DrHis>(request, response), drHis); 
		model.addAttribute("page", page);
		return "xrlh/drhis/drHisList";
	}

	@RequiresPermissions("drhis:drHis:view")
	@RequestMapping(value = "form")
	public String form(DrHis drHis, Model model) {
		model.addAttribute("drHis", drHis);
		return "xrlh/drhis/drHisForm";
	}

	@RequiresPermissions("drhis:drHis:edit")
	@RequestMapping(value = "save")
	public String save(DrHis drHis, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, drHis)){
			return form(drHis, model);
		}
		drHisService.save(drHis);
		addMessage(redirectAttributes, "保存随机抽取历史成功");
		return "redirect:"+Global.getAdminPath()+"/drhis/drHis/?repage";
	}
	
	@RequiresPermissions("drhis:drHis:edit")
	@RequestMapping(value = "delete")
	public String delete(DrHis drHis, RedirectAttributes redirectAttributes) {
		drHisService.delete(drHis);
		addMessage(redirectAttributes, "删除随机抽取历史成功");
		return "redirect:"+Global.getAdminPath()+"/drhis/drHis/?repage";
	}

}